﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Collections;

public partial class YX_BASEINFO_BS_DATASET_CLASS_Manage : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["curOperator"] = "";
            ViewState["curID"] = -1;
            BindSubSystemComb();
            BindGridData();
            EnterBrowseState();
            ShowFirstRowData();
        }
    }

    //绑定子系统下拉框
    private void BindSubSystemComb()
    {
        DataTable dt =CommonLib.BLL.SYS_SUBSYSTEM.GetAllList("SUB_SYSTEM").Tables[0];
        this.Store2.DataSource = dt;
        this.Store2.DataBind();
        SelectFristData(GridPanel1, dt, "");
    }

    //进入浏览状态

    private void EnterBrowseState()
    {
        extBtn_Add.Disabled = false;
        extBtn_Change.Disabled = false;
        extBtn_Del.Disabled = false;
        extBtn_Save.Disabled = true;
        extBtn_Cancel.Disabled = true;
        pnlEdit.Disabled = true;
        GridPanel1.Disabled = false;
    }

    //进入编辑状态

    private void EnterEditState()
    {
        extBtn_Add.Disabled = true;
        extBtn_Change.Disabled = true;
        extBtn_Del.Disabled = true;
        extBtn_Save.Disabled = false;
        extBtn_Cancel.Disabled = false;
        pnlEdit.Disabled = false;
        GridPanel1.Disabled = true;
    }

    //重置详细信息
    private void ResetEditInfo()
    {
        DataTable dt = CommonLib.BLL.SYS_SUBSYSTEM.GetList(" 1=1 ", " SUB_SYSTEM ").Tables[0];
        
        exttxtf_CLASS_NAME.SetValue("");
        exttare_REGULATOR_MODE.SetValue("");
        extrdio_1.Checked = true;
        extrdio_2.Checked = false;
        if (dt.Rows.Count > 0)
        {
            extcmbo_SUB_SYSTEM.SetValue(dt.Rows[0]["SUB_SYSTEM"].ToString());
            extcmbo_SUB_SYSTEM.SetRawValue(dt.Rows[0]["SYSTEM_NAME"].ToString());
        }
    }

    //类型名称为空或重复检查

    private bool ClassNameIsValidate(string strName)
    {
        bool result = true;
        if (String.IsNullOrEmpty(strName))
        {
            result = false;
            X.Msg.Alert("系统提示", "数据集类别名不能为空！").Show();
        }
        else
        {
            DataTable dt = CommonLib.BLL.BS_DATASET_CLASS.GetList("CLASS_NAME = '" + strName + "'").Tables[0];
            if (dt.Rows.Count > 0)
            {
                result = false;
                X.Msg.Alert("系统提示", "数据集类别名已存在，请重新输入！").Show();
            }
        }
        return result;
    }

    //绑定列表数据
    private void BindGridData()
    {
        DataTable dt = CommonLib.BLL.BS_DATASET_CLASS.GetList(" 1=1 ", " DATASET_CLASS ").Tables[0];
        this.Store1.DataSource = dt;
        this.Store1.DataBind();
    }

    //显示详细信息
    private void ShowDetail(string id)
    {
        DataTable dt = CommonLib.BLL.BS_DATASET_CLASS.GetList(" DATASET_CLASS =" + id).Tables[0];
        if (dt.Rows.Count == 0)
            return;
        exttxtf_CLASS_NAME.SetValue(dt.Rows[0]["CLASS_NAME"].ToString());
        exttare_REGULATOR_MODE.SetValue(dt.Rows[0]["CLASS_DESCR"].ToString());
        extcmbo_SUB_SYSTEM.SetValue(dt.Rows[0]["SUB_SYSTEM"].ToString());
        extcmbo_SUB_SYSTEM.SetRawValue(dt.Rows[0]["SUBSYSTEM_NAME"].ToString());
        if (dt.Rows[0]["ALLOWMODIFY"].ToString() == "是")
        {
            extrdio_1.Checked = true;
            extrdio_2.Checked = false;
        }
        if (dt.Rows[0]["ALLOWMODIFY"].ToString() == "否")
        {
            extrdio_1.Checked = false;
            extrdio_2.Checked = true;
        }
    }

    //显示第一行数据

    private void ShowFirstRowData()
    {
        DataTable dt = CommonLib.BLL.BS_DATASET_CLASS.GetList(" 1=1 ", " DATASET_CLASS ").Tables[0];
        if (dt.Rows.Count > 0)
        {
            ShowDetail(dt.Rows[0]["DATASET_CLASS"].ToString());
        }
    }

    protected void OnBtnAddClick(object sender, DirectEventArgs e)
    {
        ViewState["curOperator"] = "Add";
        ResetEditInfo();
        EnterEditState();
    }

    protected void OnBtnCancelClick(object sender, DirectEventArgs e)
    {
        if (ViewState["curOperator"].ToString() == "Add")
            ResetEditInfo();
        if (ViewState["curOperator"].ToString() == "Edit")
            ShowDetail(ViewState["curID"].ToString());
        EnterBrowseState();
    }

    protected void OnBtnChangeClick(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要修改的记录！").Show();
            return;
        }
        ViewState["curOperator"] = "Edit";
        EnterEditState();
    }

    protected void DelDataSetClass(object sender, DirectEventArgs e)
    {
        int nCount = 0;
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        nCount = sm.SelectedRows.Count;
        if (nCount == 0)
        {
            X.Msg.Alert("系统提示", "请先选择要删除的记录！").Show();
            return;
        }

        X.Msg.Confirm("系统提示", "是否删除选中行?", new MessageBoxButtonsConfig
        {
            Yes = new MessageBoxButtonConfig
            {
                Handler = "Coolite.AjaxMethods.DoDelDataSetClass();",
                Text = "是"
            },
            No = new MessageBoxButtonConfig
            {
                Text = "否"
            }
        }).Show();


    }

    [DirectMethod]
    public void DoDelDataSetClass()
    {
        RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
        foreach (SelectedRow row in sm.SelectedRows)
        {

            if (CommonLib.BLL.BS_DATASET_SQL.GetRecorderCount(" DATASET_CLASS =" + int.Parse(row.RecordID.Trim())) > 0)
            {
                X.Msg.Alert("系统提示", "该类别被引用不能删除！").Show();
                return;
            }
            try
            {
                CommonLib.BLL.BS_DATASET_CLASS.Delete(int.Parse(row.RecordID.Trim()));
            }
            catch (Exception ex)
            {
                X.Msg.Alert("系统提示", "删除记录失败！|" + ex.Message).Show();
            }
        }
        BindGridData();
        ResetEditInfo();
    }

    protected void OnBtnSaveClick(object sender, DirectEventArgs e)
    {
        if (ViewState["curOperator"].ToString() == "")
            return;
        if (ViewState["curOperator"].ToString() == "Add")
        {
            if (!ClassNameIsValidate(exttxtf_CLASS_NAME.Text.Trim()))
                return;
        }
        CommonLib.Model.BS_DATASET_CLASS oModel = new CommonLib.Model.BS_DATASET_CLASS();
        if (ViewState["curOperator"].ToString() == "Edit")
            oModel = CommonLib.BLL.BS_DATASET_CLASS.GetModel(int.Parse(ViewState["curID"].ToString()));
        oModel.CLASS_NAME = exttxtf_CLASS_NAME.Text.Trim();
        oModel.CLASS_DESCR = exttare_REGULATOR_MODE.Text.Trim();
        if (extrdio_1.Checked)
            oModel.MODIFY_FLAG = 1;
        if (extrdio_2.Checked)
            oModel.MODIFY_FLAG = 0;
        oModel.SUB_SYSTEM = short.Parse(extcmbo_SUB_SYSTEM.SelectedItem.Value.ToString());

        if (ViewState["curOperator"].ToString() == "Add")
        {
            oModel.DATASET_CLASS = CommonLib.BLL.BS_DATASET_CLASS.GetNewID();
            try
            {
                CommonLib.BLL.BS_DATASET_CLASS.Add(oModel);
                X.Msg.Alert("系统提示", "保存成功！").Show();
                ViewState["curOperator"] = "";
                EnterBrowseState();
                ResetEditInfo();
            }
            catch (Exception ex)
            {
                X.Msg.Alert("系统提示", "保存失败！|" + ex.Message).Show();
            }
        }
        else
        {
            if (ViewState["curOperator"] as string == "Edit")
            {
                try
                {
                    CommonLib.BLL.BS_DATASET_CLASS.Update(oModel);
                    X.Msg.Alert("系统提示", "保存成功！").Show();
                    ViewState["curOperator"] = "";
                    EnterBrowseState();
                    ShowDetail(ViewState["curID"].ToString());
                }
                catch (Exception ex)
                {
                    X.Msg.Alert("系统提示", "保存失败！|" + ex.Message).Show();
                }
            }
        }

        BindGridData();
    }

    protected void OnRowClick(object sender, DirectEventArgs e)
    {
        ViewState["curID"] = int.Parse(e.ExtraParams["curID"]);
        ShowDetail(ViewState["curID"].ToString());
    }

    protected void OnShowFirstRowData(object sender, DirectEventArgs e)
    {
        ShowFirstRowData();
    }
}
